package com.wyj;

import com.google.protobuf.InvalidProtocolBufferException;
import com.wyj.dao.UserDao;
import com.wyj.pojo.GameUser;
import com.wyj.protoBuf.protoJava.GameUserProto;
import com.wyj.protoBuf.protoJava.ListProto;
import com.wyj.protoBuf.protoJava.PerSonHaveCar;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Arrays;

@SpringBootTest
public class ProtoBuffTexts {

    @Autowired
    UserDao userDao;

    //java当中的普通序列化 将java对象转换为字节数组进行传输  或者是将字节数组转换为java对象
    public static byte[] Serialization (GameUser gameUser) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ObjectOutputStream stream = new ObjectOutputStream(out);

        stream.writeObject(gameUser);

        byte[] bytes = out.toByteArray();
        return bytes;
    }

    //测试java的序列化
    @Test
    void text01() throws IOException {
        GameUser gameUserById = userDao.findGameUserById(1L);

        byte[] serialization = Serialization(gameUserById);

        System.out.println(Arrays.toString(serialization));
    }

    /**
     * 测试protoBuf
     *
     * 对象数据序列化  反序列化
     */
    @Test
    void text02() throws InvalidProtocolBufferException {
        //新建对象并对属性进行赋值
        GameUserProto.GameUser gameUser = GameUserProto.GameUser.newBuilder()
                .setId(1L)
                .setGoldCoin(1000L)
                .setUserLevel(9999L)
                .setUserName("wyj")
                .setUUID("saaadxs")
                .setVipLevel(90900L)
                .build();

        //将对象序列化为字节数组
        byte[] bytes = gameUser.toByteArray();

        //将字节数组反序列化为 对象
        GameUserProto.GameUser gameUser1 = GameUserProto.GameUser.parseFrom(bytes);

        System.out.println(Arrays.toString(bytes));

        System.out.println("由protoBuf生成的对象:"+gameUser);
        System.out.println("反序列化生成对象："+gameUser1);

    }

    /**
     * 测试protoBuf
     * 将List集合进行序列化 反序列化
     */
    @Test
    void text03() throws InvalidProtocolBufferException {
        //获取到List
        ListProto.Names getListNames = ListProto.Names.newBuilder()
                .addName("wyj1")
                .addName("wyj2")
                .addName("wyj3")
                .addName("wyj4")
                .addName("wyj5")
                .build();

        //将List序列化为字节数组
        byte[] bytes = getListNames.toByteArray();
        System.out.println(Arrays.toString(bytes));

        //将字节数组反序列化为 List
        ListProto.Names names = ListProto.Names.parseFrom(bytes);

        System.out.println(names.getName(0));
        System.out.println(names.getName(1));
        System.out.println(names.getName(2));
        System.out.println(names.getName(3));
        System.out.println(names.getName(4));

    }

    /**
     * 测试protoBuf
     * 将map集合序列化 反序列化
     */
    @Test
    void text04() throws InvalidProtocolBufferException {
//        //给map集合中进行赋值
//        MapProto.NameAndAge nameAndAge = MapProto.NameAndAge.newBuilder()
//                .setField("wyj1",1)
//                .setField("wyj2",2)
//                .setField("wyj3",3)
//                .build();
//
//        //将map集合进行序列化为字节数组
//        byte[] bytes = nameAndAge.toByteArray();
//        System.out.println(Arrays.toString(bytes));
//        //将字节数组进行反序列化为map集合
//        MapProto.NameAndAge nameAndAge1 = MapProto.NameAndAge.parseFrom(bytes);
//
//        System.out.println(nameAndAge1.containsMap("wyj1"));
//        System.out.println(nameAndAge1.containsMap("wyj2"));
//        System.out.println(nameAndAge1.containsMap("wyj3"));
    }

    /**
     * 测试proto
     * 将嵌套的消息序列化
     */
    @Test
    void text05() throws InvalidProtocolBufferException {

        //创建出需要序列化的对象，如果在开发当中，这里边赋值可能就是我们从真正的对象当中进行获取
        PerSonHaveCar.persons person = PerSonHaveCar.persons.newBuilder()
                .setAge(String.valueOf(12))
                .setName("name")
                .addCar(
                    PerSonHaveCar.persons.cars.newBuilder()
                            .setName("大众")
                            .setFromCountry("美国")
                            .build()
                )
                .build();

        //将嵌套对象序列为字节数组
        byte[] bytes = person.toByteArray();
        System.out.println(Arrays.toString(bytes));

        //将字节数组解析为嵌套对象
        PerSonHaveCar.persons persons = PerSonHaveCar.persons.parseFrom(bytes);
        System.out.println(persons.getAge());
        System.out.println(persons.getName());
        //处理嵌套对象
        PerSonHaveCar.persons.cars car = persons.getCar(0);

        System.out.println(car.getName()) ;
        System.out.println(car.getFromCountry());


    }




}
